package com.service.abolishing.service.impl;

import com.service.abolishing.dao.ApMacDao;
import com.service.abolishing.dao.JsonDataDao;
import com.service.abolishing.dao.TerminalListDao;
import com.service.abolishing.dto.Jsondata;
import com.service.abolishing.dto.NceUploadData;
import com.service.abolishing.entity.CloudCampusInfo;
import com.service.abolishing.entity.JsonData;
import com.service.abolishing.entity.TerminalList;
import com.service.abolishing.service.CloudCampusInfoService;
import com.service.abolishing.service.JsonDataService;
import com.service.abolishing.service.LocationResultService;
import com.service.config.utils.IdUtil;
import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.UuIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 上报数据表(JsonData)表服务实现类
 *
 * @author makejava
 * @since 2021-10-21 14:14:35
 */
@Service("jsonDataService")
@Slf4j
public class JsonDataServiceImpl implements JsonDataService {
    @Resource
    private JsonDataDao jsonDataDao;
    @Resource
    ApMacDao apMacDao;
    @Resource
    TerminalListDao terminalListDao;
    @Resource
    LocationResultService locationResultService;
    @Resource
    CloudCampusInfoService cloudCampusInfoService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JsonData queryById(String id) {
        return this.jsonDataDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<JsonData> queryAllByLimit(int offset, int limit) {
        return this.jsonDataDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param jsonData 实例对象
     * @return 实例对象
     */
    @Override
    public JsonData insert(JsonData jsonData) {
        this.jsonDataDao.insert(jsonData);
        return jsonData;
    }

    /**
     * 修改数据
     *
     * @param jsonData 实例对象
     * @return 实例对象
     */
    @Override
    public JsonData update(JsonData jsonData) {
        this.jsonDataDao.update(jsonData);
        return this.queryById(jsonData.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.jsonDataDao.deleteById(id) > 0;
    }

    @Override
    public void insertList(NceUploadData lbsEntity, CloudCampusInfo cloudCampusInfo) throws Exception {
        List<TerminalList> terminalLists = new ArrayList<TerminalList>();
        String jsonId = IdUtil.getId().toString();
        Jsondata jsondata = new Jsondata();
        jsondata.setId(jsonId);
        jsondata.setData(JsonXMLUtils.obj2json(lbsEntity.getData()));
        jsondata.setTimestamp(new Date());
        //将终端位置信息输出至csv文件，输出完成后关闭
        lbsEntity.getData().forEach(apUploadData -> {
            apUploadData.getTerminalList().forEach(terminalData -> {
                TerminalList terminallist = new TerminalList();
                terminallist.setId(IdUtil.getId().toString());
                terminallist.setApmacid(jsondata.getId());
                terminallist.setTerminalmac(terminalData.getTerminalMac());
                terminallist.setApmac(apUploadData.getApMac());
                terminallist.setMacrssi(UuIDUtil.Random2(terminalData.getRssi()));
                terminallist.setTimestamp(String.valueOf(terminalData.getTimestamp()));
                terminalLists.add(terminallist);
            });
        });
        //每一分钟发送的数据做插入操作，不做更新
        JsonData jsonData = ModelMapperUtil.strictMap(jsondata, JsonData.class);
        jsonData.setId(IdUtil.getStringId());
        jsonData.setManageInfoId(cloudCampusInfo.getManageId());
        jsonDataDao.insert(jsonData);
        if (terminalLists.size()>0){
            terminalListDao.insertBatch(terminalLists);
            log.info("begin");
            locationResultService.getLocationResult(terminalLists,jsonData.getManageInfoId());
            log.info("end");
        }
    }
}
